package com.Jshop.upload.controller;

import com.Jshop.upload.Service.UploadService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("upload")
public class UploadController {

    @Autowired
    private UploadService uploadService;

    // 返回String:图片路径
    //参数为图片文件 SpringMVC封装成MultipartFile
    @PostMapping("image")
    public ResponseEntity<String> uploadImage(@RequestParam("file") MultipartFile file){

        String upload = this.uploadService.upload(file);

        if (StringUtils.isBlank(upload)){
            return ResponseEntity.badRequest().build();
        }
        //响应Post成功，返回图片路径
        return ResponseEntity.status(HttpStatus.CREATED).body(upload);

    }
}
